//对请求参数进行解密并输出日志
//读取json文件作为返回参数，加密并输出日志
var fs = require('fs');
var crypt = require("./crypt");
exports.api = {
    // var data = function(req, path){
    reqBody:function(req){
        console.log("请求header数据为：" + JSON.stringify(req.headers));        
        var body = '';
        var reqBody = '';
        //get请求
        if(req.method == "GET"){
            //获取请求param数据
            if(req.originalUrl.indexOf('?') > 0){//判断有？
                body = req.originalUrl.split("?")[1].split("=")[1];
            }
        }else if(req.method == "POST"){
            body = req.body["param"];
        }
        //解密
        console.log("加密请求参数为："+JSON.stringify(body));
        try{
            reqBody = crypt.des.decrypt(body);
            console.log("未加密的请求参数为:" + JSON.stringify(reqBody));
        }catch(e){
            console.log(e.stack);
            console.log('{"code":"1","message":"请求参数有误"}');
            // return '{"code":"1","message":"请求参数有误"}';
        }
        return reqBody;
    },
    // }
    request:function(path){       
        var data = fs.readFileSync(path,'utf-8');
        var resBody = JSON.parse(data.toString());
        console.log("未加密的返回值为：" + JSON.stringify(resBody));
        var arrData = resBody["data"];
        var encryptData = crypt.des.encrypt(JSON.stringify(arrData));
        resBody["data"] = encryptData;
        console.log("加密后的返回值为：" + JSON.stringify(resBody));
        return resBody;
    },
    urltoPath:function(url){
        var pathAry = url.split("/");
        return __dirname + "/../public/json/" + pathAry[3] + "/" + pathAry[4];

        
    }

}

